<?php
/**
 * Created by PhpStorm.
 * User: xinghuo
 * Date: 2017/7/18
 * Time: 下午9:23
 */

namespace App\Http\Controllers;


use App\Library\WxPayCallback;
use App\Models\Order;
use App\Models\UserProduct;
use Illuminate\Http\Request;
use Input;
use Log;

class PayNotifyController
{
    public function wechat(Request $request)
    {

        Log::notice('pay: This Request is Payment', ['data' => $request->getContent()]);


        $response = app('wechat')->payment->handleNotify(function ($notify, $successful) {

            $trade_no = $notify->out_trade_no;
            $out_trade_no = $notify->transaction_id;
            $order = Order::whereSn($trade_no)->first();
            $price = $notify->total_fee;

            if (!$order) {
                return false;
            }

            $result = Order::doPay($order, Order::PAY_TYPE_WECHAT, $out_trade_no, $price / 100);
            if ($result) {
                app('log')->debug('wechat pay success');
                return true;
            } else {
                return false;
            }

        });

        return $response;


    }

    public function wechatApp(Request $request)
    {

        $notify = new WxPayCallback($this);
        $notify->Handle(true);
    }

    public function WxPayNotifyProcess($data,$msg)
    {

        $trade_no = $data['out_trade_no'];
        $transaction_id = $data['transaction_id'];
        $total_fee = $data['total_fee'];
        $out_trade_no = $transaction_id;
        $order = Order::whereSn($trade_no)->first();
        $price = $total_fee;

        if (!$order) {
            return false;
        }

        $result = Order::doPay($order, Order::PAY_TYPE_WECHAT, $out_trade_no, $price / 100);
        if ($result) {
            app('log')->debug('wechat pay success');
            return true;
        } else {
            return false;
        }
    }

    public function alipayApp(Request $request)
    {

        $aop = new \AopClient;
        Log::debug("alipay ", $request->all());
        $aop->alipayrsaPublicKey = str_replace("\r\n", "", file_get_contents(config('alipay.alipay_public_key')));
        $flag = $aop->rsaCheckV1($request->all(), NULL, "RSA2");
        if ($flag) {
            Log::debug("alipay pass " . $request->input("out_trade_no"));
            $sn = $request->input("out_trade_no");
            $total_amount = $request->input("total_amount");
            $order = Order::whereSn($sn)->first();

            if (!$order) {
                Log::debug('Alipay Order Miss', [
                    'out_trade_no' => $request->get('out_trade_no'),
                    'trade_no' => $request->get('trade_no')
                ]);
                return '';
            }
            // 判断通知类型。
            switch ($request->get('trade_status')) {
                case 'TRADE_SUCCESS':
                case 'TRADE_FINISHED':
                    // TODO: 支付成功，取得订单号进行其它相关操作。
                    Log::debug('Alipay notify post data verification success.', [
                        'out_trade_no' => $request->get('out_trade_no'),
                        'trade_no' => $request->get('trade_no')
                    ]);
                    if (Order::doPay($order, Order::PAY_TYPE_ALIPAY, $request->get('out_trade_no'), $total_amount)) {
                        app('log')->debug('alipay success');
                        echo 'success';
                        break;
                    }
            }
        } else {
            return 'fail';
        }
    }


}